package ldh.im.fxgui;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ScreenCaptureDemo {
    private JFrame frmScreencapture;
    private JButton play, stop, capture;
    private File createFolder;
    private Rectangle rectangle;
//    private XWPFDocument document;
//    private XWPFParagraph paragraph;
//    private XWPFRun run;
    private BufferedImage bufferedImage;
    private String foldername = null;
    private String filename = null;
    private Boolean folderclearflag = true;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    ScreenCaptureDemo window = new ScreenCaptureDemo();
                    window.frmScreencapture.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ScreenCaptureDemo() {
        initialize();

    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmScreencapture = new JFrame();
//        frmScreencapture.setIconImage(Toolkit.getDefaultToolkit().getImage(ScreenCaptureDemo.class.getResource("/com/org/screencapture/code.png")));
        frmScreencapture.setTitle("Screen Capture");
        frmScreencapture.setBounds(5, 30, 295, 129);
        frmScreencapture.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmScreencapture.setResizable(false);
        frmScreencapture.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                if (e.getKeyCode() == KeyEvent.VK_PRINTSCREEN) {
                    capture();
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
            }
        });
        frmScreencapture.setFocusable(true);
        frmScreencapture.setFocusTraversalKeysEnabled(false);

        play = new JButton("play");
        play.setToolTipText("Start the App - Creates a folder to store captured screenshots");
        play.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start();
            }
        });
//        play.setIcon(new ImageIcon(ScreenCaptureDemo.class.getResource("/com/org/screencapture/google-play.png")));

        stop = new JButton("stop");
        stop.setToolTipText("Save the captured screenshots in word and pdf document");
        stop.setEnabled(false);
        stop.setForeground(Color.BLACK);
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    JFileChooser filechooser = new JFileChooser();
                    int option = filechooser.showSaveDialog(frmScreencapture);
                    if (option == JFileChooser.APPROVE_OPTION) {
                        File savefile = filechooser.getSelectedFile();
                        filename = savefile.getAbsolutePath();
                    } else {
                        String docname = "Screenshot-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
                        filename = foldername + "/" + docname;
                        folderclearflag = false;
                        JOptionPane.showMessageDialog(null, "User clicked cancel output word and PDF files will be stored in the root directory of application", "Save Warning", JOptionPane.WARNING_MESSAGE);
                    }
//                    createPDFDoc(filename);
//                    createWordDoc(filename);
                    fileaction(folderclearflag);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
//        stop.setIcon(new ImageIcon(ScreenCaptureDemo.class.getResource("/com/org/screencapture/save.png")));

        capture = new JButton("capture");
        capture.setToolTipText("Capture Screenshots");
        capture.setEnabled(false);
//        capture.setIcon(new ImageIcon(ScreenCaptureDemo.class.getResource("/com/org/screencapture/video.png")));
        capture.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                capture();
            }
        });

        GroupLayout groupLayout = new GroupLayout(frmScreencapture.getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(8)
                                .addComponent(play, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
                                .addGap(10)
                                .addComponent(stop, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(capture, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
                                .addGap(24))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(GroupLayout.Alignment.TRAILING, groupLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(capture, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(stop, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(play, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addContainerGap())
        );
        frmScreencapture.getContentPane().setLayout(groupLayout);
    }

    private void start() {
        foldername = "/Screenshots_" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
        createFolder = new File(foldername);
        createFolder.mkdir();
        play.setEnabled(false);
        capture.setEnabled(true);
        frmScreencapture.setAlwaysOnTop(true);
    }

    private void capture() {
        stop.setEnabled(true);
        if (createFolder.exists() && createFolder.isDirectory()) {
            try {
                frmScreencapture.setVisible(false);
                Thread.sleep(500);
                Robot robot = new Robot();
                rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
                bufferedImage = robot.createScreenCapture(rectangle);
                String fileSuffix = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
                File createImg = new File(foldername + "/" + fileSuffix + ".png");
                ImageIO.write(bufferedImage, "png", createImg);
                frmScreencapture.setVisible(true);
            } catch (Exception e) {
                System.err.println(e);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Screen Capture", "Warning Screenshot folder does not exist", JOptionPane.WARNING_MESSAGE);
        }

    }

//    private void createWordDoc(String folder) throws IOException, InvalidFormatException {
//        document = new XWPFDocument();
//        //String docname = "Screenshot-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//        FileOutputStream out = new FileOutputStream(new File(folder + ".docx"));
//        File[] files = createFolder.listFiles();
//        paragraph = document.createParagraph();
//        run = paragraph.createRun();
//        int picformat = org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_PNG;
//        for (File file : files) {
//            if (FilenameUtils.getExtension(file.getName()).equalsIgnoreCase("png")) {
//                FileInputStream fis = new FileInputStream(file);
//                run.addPicture(fis, picformat, file.getName(), Units.toEMU(470), Units.toEMU(300));
//                run.addBreak(BreakType.TEXT_WRAPPING);
//                fis.close();
//            }
//        }
//        document.write(out);
//        out.close();
//        document.close();
//    }

//    private void createPDFDoc(String filename) throws IOException {
//        //String docname = "Screenshot-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//        PdfWriter writer = new PdfWriter(filename + ".pdf");
//        PdfDocument pdfdoc = new PdfDocument(writer);
//        pdfdoc.addNewPage();
//        Document pdfdocument = new Document(pdfdoc);
//        File[] imgfiles = createFolder.listFiles();
//        @SuppressWarnings("deprecation")
//        Table table = new Table(1);
//        table.setBorder(Border.NO_BORDER);
//        for (File file : imgfiles) {
//            if (FilenameUtils.getExtension(file.getName()).equalsIgnoreCase("png")) {
//                Cell cell = new Cell();
//                cell.setBorder(Border.NO_BORDER);
//                ImageData data = ImageDataFactory.create(file.getPath());
//                Image image = new Image(data);
//                image.setWidthPercent(100);
//                cell.add(image);
//                table.addCell(cell);
//            }
//        }
//        pdfdocument.add(table);
//        pdfdocument.close();
//        writer.close();
//    }

    private void fileaction(Boolean folderflag) {
        frmScreencapture.setVisible(false);
        File[] srcimage = createFolder.listFiles();
        for (File imgfile : srcimage) {
            if (imgfile.getName().toLowerCase().endsWith("png")) {
                String path = imgfile.getAbsolutePath();
                File delfile = new File(path);
                delfile.delete();
            }
        }
        if (folderflag) {
            createFolder.delete();
        }
        JOptionPane.showMessageDialog(null, "Process Completed and Output file saved as PDF and Doc");
        frmScreencapture.dispose();
    }
}
